/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AccesoDatos;
import java.sql.*;
import java.util.logging.*;
import java.util.*;
/**
 *
 * @author dani
 */
public class Vista {
    
    private ResultSet res;  
    private List<Dato> campo=new ArrayList<Dato>();
    private int nr;
    public Vista(ResultSet r) {
        this.res=r;
        this.construirCampos();
    }   
    
    public List<Dato> Campos() {
        return this.campo;
    }
    
    public int getNCampos() {
        try {
            return this.res.getMetaData().getColumnCount();            
        } catch (SQLException ex) {
            Logger.getLogger(ConMySQL.class.getName()).log(Level.SEVERE, null, ex);
            return 0;
        }
    }   
    
    private void construirCampos() {
        try {
            int c=0;
            while (this.res.next()) {
                for(int i=0;i<this.getNCampos();i++)                
                    this.campo.add(new Dato(this.getNombreCampo(i), this.res.getString(this.getNombreCampo(i))));
                c++;
            }
            this.nr=c;
        } catch (SQLException ex) {
            Logger.getLogger(Vista.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public int getNRegistros() {
        return this.nr;
    }
    
    public Object getRegistro(int f, int c) {
        try {
            
            this.res.absolute(f+1);
             return this.res.getObject(c+1);         
            
        } catch (SQLException ex) {
            Logger.getLogger(Vista.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    private String getNombreCampo(int i) {
        try {
            return this.res.getMetaData().getColumnName(i+1);            
        } catch (SQLException ex) {
            Logger.getLogger(ConMySQL.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    } 
}
